<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>
            <button data-target="../user/select.html" class="layui-btn layui-btn-sm" id="retur">返回</button>
            添加用户
        </legend>
    </fieldset>

    <form class="layui-form" action="" onsubmit="return false">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">手机|账号</label>
                <div class="layui-input-inline">
                    <input type="tel" name="mobile" lay-verify="required" placeholder="请输入手机号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="text" name="pwd" lay-verify="required|number" placeholder="请输入密码" autocomplete="off"
                        class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">管理员</label>
                <div class="layui-input-block">
                    <input type="checkbox" value="true" name="isAdmin" lay-skin="switch">
                    <div class="layui-unselect layui-form-switch" lay-skin="_switch"><em></em><i></i></div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="course-add">添加用户</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script src="../../js/lodash.min.js"></script>
    <script src="../../js/mock-min.js"></script>
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.12.4.min.js"></script>
    <script src="../../js/index.js"></script>
    <script src="../../js/common.js"></script>
    <script>
        layui.use(['form'], function () {
            const form = layui.form;

            //监听提交
            form.on('submit(course-add)', async function (data) {
                // if (!data.field.isAdmin) {
                //     data.field.isAdmin = false;
                // }
                console.log(data.field.isAdmin)
                await $.post("/api/User/post", data.field);
                layer.msg('添加用户成功', {
                    icon: 1,
                    time: 1000
                }, function () {
                    location.href = "./select.html";
                });
            });


            //设置开关按钮
            // form.on('switch(isAdmin)', function (obj) {
            //     console.log(obj)
            //     console.log(obj.elem)
            //     console.log(obj.elem.checked)
            //     $.ajax({
            //         url: "/api/User/" + obj.value,
            //         method: "put",
            //         data: {
            //             isAdmin: obj.elem.checked
            //         }
            //     })
            // })
        });
        let retur = document.getElementById("retur");
        retur.onclick = function () {
            location.href = "./select.html";
        }
    </script>
    <script type="textml" id="swicthTp">
        <input type="checkbox" name="isAdmin" value="{{d._id}}" lay-skin="switch" lay-text="是|否" lay-filter="isAdmin" data-filter="isAdmin" {{d.isAdmin == true ? 'checked' : ''}} >
</script>
</body>

</html>